﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace WebApplication2
{
    public partial class MoreGrids : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblError.Text = "";
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "AddNew")
            {
                GridView1.ShowFooter = true;        // Turn ON the grid's footer
            }
            else if (e.CommandName == "Insert")
            {
                // Write code for inserting data

                // Gather new data
                TextBox txtCompany = GridView1.FooterRow.FindControl("txtNewCompanyName") as TextBox;
                string newCompanyName = txtCompany.Text;

                TextBox txtPhone = GridView1.FooterRow.FindControl("txtNewPhone") as TextBox;
                string newPhone = txtPhone.Text;

                try
                {
                    SqlDataSource1.InsertParameters.Clear();
                    SqlDataSource1.InsertParameters.Add("CompanyName", newCompanyName);
                    SqlDataSource1.InsertParameters.Add("Phone", newPhone);

                    SqlDataSource1.Insert();

                    GridView1.ShowFooter = false;
                }
                catch (Exception ex)
                {
                    lblError.Text = ex.Message;
                }
            }
        }

        protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            if (e.Exception != null)
            {
                // An error has occured
                lblError.Text = e.Exception.Message;
                e.ExceptionHandled = true;
                e.KeepInEditMode = true;
            }
        }
    }
}